export default {
	toHex:function(n) {
		return `${n > 15 ? '' : 0}${n.toString(16)}`
	},
	parseHexColor:function(color) {
		let hex = color.slice(1);
		let a = 1;
		if (hex.length === 3) {
			hex = `${hex[0]}${hex[0]}${hex[1]}${hex[1]}${hex[2]}${hex[2]}`;
		}
		if (hex.length === 8) {
			a = parseInt(hex.slice(6), 16) / 255;
			hex = hex.slice(0, 6);
		}
		const bigint = parseInt(hex, 16);
		return {
			r: (bigint >> 16) & 255,
			g: (bigint >> 8) & 255,
			b: bigint & 255,
			a,
		}
	},
	parseRgbaColor:function(color) {
	  const arr = color.match(/(\d(\.\d+)?)+/g) || [];
	  const res = arr.map((s) => parseInt(s, 10));
	  return {
	    r: res[0],
	    g: res[1],
	    b: res[2],
	    a: parseFloat(arr[3]),
	  }
	},
	parseColorString:function(color) {
	  if (color.startsWith('#')) {
	    return parseHexColor(color);
	  } else if (color.startsWith('rgb')) {
	    return parseRgbaColor(color);
	  }
	},
	toRgbaString:function(colorObj) {
	  const { r, g, b, a } = colorObj;
	  return `rgba(${r},${g},${b},${a})`;
	};
}